package own.stu.jobgib.playown.voiceNoval;

import jodd.net.HtmlDecoder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import own.stu.jobgib.playown.vediodown.common.service.BaseDownloadService;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//import java.net.URLDecoder;
//import java.net.URLEncoder;

public class LoadVoice {

    // https://www.lrts.me/ajax/book/34396448/69/10


    public static void main(String[] args) throws Exception {
        LoadVoice load = new LoadVoice();
        load.loadLogic();

       /* File file = new File(path);
        if(file.isDirectory()){
            for (File f : file.listFiles()) {
                File nF = new File(f.getName() + ".mp3");
                f.renameTo(nF);
            }
        }*/
    }

    RestTemplate template = new RestTemplate();

    private void loadLogic() {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        int start = 69;
        int end = 78;
        String urlFormat = "https://www.lrts.me/ajax/book/34396448/%d/10";
        for (int i = start; i <= end; i++) {
            String url = String.format(urlFormat, i);
            System.out.println(url);
            ResponseEntity<Result> response = template.getForEntity(url, Result.class);
            if (HttpStatus.OK.equals(response.getStatusCode())) {
                Result result = response.getBody();
                if (result.getData() == null || result.getData().getData() == null) {
                    continue;
                }
                List<DataItem> items = result.getData().getData();

                for (DataItem item : items) {
                    long resId = item.getResId();
                    String resName = htmlDecode(item.getResName());
                    System.out.println(resName);
                    executorService.submit(() -> {
                        downLoad(resId, resName);
                    });
                    // downLoad(resId, resName);
                }
            }
        }

        executorService.shutdown();
    }

    private void downLoad(long id, String name) {
        String url = String.format(voiceUlr, id + "");
        ResponseEntity<VoiceItem> response = template.getForEntity(url, VoiceItem.class);
        if (HttpStatus.OK.equals(response.getStatusCode())) {
            VoiceItem item = response.getBody();
            if (item != null && StringUtils.isNotBlank(item.getData())) {
                String voiceUrl = item.getData();
                BaseDownloadService.process("wget", "-O", path + "/" + name + ".mp3", voiceUrl);
            }
        }
    }

    String voiceUlr = "https://www.lrts.me/ajax/path/4/34396448/%s";
    static String path = "/Users/qim/Desktop/voice_noval";

    /**
     * @param string
     * @return 转换之后的内容
     * @Title: unicodeDecode
     * @Description: unicode解码 将Unicode的编码转换为中文
     */
    public static String unicodeDecode(String string) {
        Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
        Matcher matcher = pattern.matcher(string);
        char ch;
        while (matcher.find()) {
            ch = (char) Integer.parseInt(matcher.group(2), 16);
            string = string.replace(matcher.group(1), ch + "");
        }
        return string;
    }

    public static String htmlDecode(String str) {
        return HtmlDecoder.decode(str);
    }
}
